﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Zip;

namespace SharpZipDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDecompress_Click(object sender, EventArgs e)
        {
            if (txtopenzipfile.Text.Trim() == "") return;
            if (folderBrowserDialog1.ShowDialog() != DialogResult.OK) return;
            if (Decompress(txtopenzipfile.Text.Trim(), folderBrowserDialog1.SelectedPath))
                MessageBox.Show("解压文件成功");
            folderBrowserDialog1.Dispose();
        }

        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCompress_Click(object sender, EventArgs e)
        {
            if (txtopenfile.Text.Trim() == "") return;
            string filename = txtopenfile.Text.Trim().Substring(txtopenfile.Text.Trim().LastIndexOf("\\") + 1);
            saveFileDialog1.FileName = filename.Remove(filename.LastIndexOf("."));
            saveFileDialog1.Filter = "ECG|*.ECG";
            if (saveFileDialog1.ShowDialog() != DialogResult.OK) return;
            if (Compress(txtopenfile.Text.Trim(), saveFileDialog1.FileName))
                MessageBox.Show("压缩文件成功");
            saveFileDialog1.Dispose();
        }

        private void btnopen_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "选择要压缩的文件";
            openFileDialog1.Filter = "所有文件|*.*";
            if (openFileDialog1.ShowDialog() != DialogResult.OK) return;
            txtopenfile.Text = openFileDialog1.FileName;
            openFileDialog1.Dispose();
        }

        private void btnopenzip_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "选择要解压的文件";
            openFileDialog1.Filter = "所有文件|*.ECG";
            if (openFileDialog1.ShowDialog() != DialogResult.OK) return;
            txtopenzipfile.Text = openFileDialog1.FileName;
            openFileDialog1.Dispose();
        }


        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="filespath">要压缩的文件</param>
        /// <param name="zipfilename">压缩文件保存路径</param>
        /// <returns>bool</returns>
        public static bool Compress(string filespath, string zipfilename)
        {
            try
            {
                using (ZipFile zip = ICSharpCode.SharpZipLib.Zip.ZipFile.Create(zipfilename))
                {
                    zip.BeginUpdate();
                    zip.Add(filespath, filespath.Substring(filespath.LastIndexOf("\\") + 1));
                    zip.CommitUpdate();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
            return true;
        }

        /// <summary>
        /// 解压文件
        /// </summary>
        /// <param name="zipfilename">要解压的文件</param>
        /// <param name="filespath">解压路径</param>
        /// <returns>bool</returns>
        public static bool Decompress(string zipfilename, string filespath)
        {
            try
            {
                //获取系统临时目录
                //string sysTempDir = Path.GetTempPath();
                // ZipEntry zipEntry = new ZipFile(zipfilename)[0];
                new FastZip().ExtractZip(zipfilename, filespath, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
            return true;
        }
    }
}
